package com.sinosoft.dsp.platform.components.portal.iteration;

import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;

import org.primefaces.event.SelectEvent;

import com.sinosoft.dsp.platform.exception.SinoPlatformException;
import com.sinosoft.dsp.platform.utils.MessageBundles;
import com.sinosoft.dsp.platform.utils.SinoManagedBean;
import com.sinosoft.dsp.system.utils.Const;

/**
 * 抽象的数据表格ViewBean
 * 
 * @author 潘巍（Peter Pan）
 * @since 2011-5-8 上午09:24:53
 */
public abstract class SinoAbstractDataTableViewBean<T extends IDataTableObject>
		extends AbstractIterationViewBean<T> implements
		ISinoDataTableViewBean<T> {

	// 视图类型的选择项
	public final static List<SelectItem> viewTypeSelectItems = new ArrayList<SelectItem>();

	/**
	 * 选择方式
	 */
	private DataTableSelectionModeType selectionMode;

	/**
	 * 表格视图类型（grid，table）
	 */
	private DataTableViewType viewType;

	/**
	 * 显示行数
	 */
	private String rowsNumber = String.valueOf(Const.DEFAULT_PAGE_SIZE);

	/**
	 * 是否分页
	 */
	private boolean paginator = true;

	/**
	 * 是否列可以调整宽度
	 */
	private boolean resizableColumns = true;

	/**
	 * 行内容
	 */
	public List<T> rows;

	/**
	 * 选中的行
	 */
	public T selectedRow;
	public T[] selectedRows;

	/**
	 * 数据表格的表头对象
	 */
	public List<DataTableHeaderObject> header;

	@Override
	public String getSelectionMode() {
		if (selectionMode == null) {
			selectionMode = DataTableSelectionModeType.SINGLE;
		}
		return selectionMode.getValue();
	}

	@Override
	public void setSelectionMode(String selectionMode) {
		this.selectionMode = DataTableSelectionModeType.getType(selectionMode);
	}

	@Override
	public String getViewType() {
		if (viewType == null) {
			viewType = DataTableViewType.TABLE;
		}
		return viewType.getValue();
	}

	@Override
	public void setViewType(String viewType) {
		this.viewType = DataTableViewType.getType(viewType);
	}

	@Override
	public String getRowsNumber() {
		return rowsNumber;
	}

	@Override
	public void setRowsNumber(String rowsNumber) {
		this.rowsNumber = rowsNumber;
	}

	@Override
	public boolean isPaginator() {
		return paginator;
	}

	@Override
	public void setPaginator(boolean paginator) {
		this.paginator = paginator;
	}

	@Override
	public boolean isResizableColumns() {
		return resizableColumns;
	}

	@Override
	public void setResizableColumns(boolean resizableColumns) {
		this.resizableColumns = resizableColumns;
	}

	@Override
	public void addRow(T row) throws SinoPlatformException {
		try {
			rows.add(row);
		} catch (Exception e) {
			throw new SinoPlatformException("添加行记录出现错误");
		}
	}

	@Override
	public void addRow(int index, T row) throws SinoPlatformException {
		try {
			rows.add(index, row);
		} catch (Exception e) {
			throw new SinoPlatformException("添加行记录出现错误");
		}
	}

	@Override
	public void addRows(List<T> rows) throws SinoPlatformException {
		if (rows != null) {
			for (T t : rows) {
				addRow(t);
			}
		}
	}

	@Override
	public void delRow(String id) throws SinoPlatformException {
		try {
			if (Const.isEmpty(id)) {
				for (T t : rows) {
					if (id.equals(t.getId())) {
						rows.remove(t);
						break;
					}
				}
			}
		} catch (Exception e) {
			throw new SinoPlatformException("删除行记录出现错误");
		}
	}

	@Override
	public T getRow(String id) {
		if (Const.isEmpty(id)) {
			for (T t : rows) {
				if (id.equals(t.getId())) {
					return t;
				}
			}
		}
		return null;
	}

	@Override
	public List<SelectItem> getViewTypeSelectItems() {
		if (viewTypeSelectItems.size() == 0) {
			MessageBundles mb = SinoManagedBean.findBean("messageBundles");
			// 视图类型-列表
			viewTypeSelectItems
					.add(new SelectItem(DataTableViewType.TABLE.getValue(), mb
							.getValue(DataTableViewType.TABLE.getText())));

			// 视图类型-文本
			viewTypeSelectItems
					.add(new SelectItem(DataTableViewType.GRID.getValue(), mb
							.getValue(DataTableViewType.GRID.getText())));
		}
		return viewTypeSelectItems;
	}

	@Override
	public String getRowsNumberSelectItems() {
		if (DataTableViewType.GRID == viewType) {
			return ISinoDataTableViewBean.SINO_VIEW_TYPE_GRID_ROW_NUMBER;
		}
		return ISinoDataTableViewBean.SINO_VIEW_TYPE_TABLE_ROW_NUMBER;
	}

	@Override
	public T[] getSelectedRows() {
		return selectedRows;
	}

	@Override
	public void setSelectedRows(T[] rows) {
		this.selectedRows = rows;
	}

	@Override
	public T getSelectedRow() {
		return selectedRow;
	}

	@Override
	public void setSelectedRow(T selectedRow) {
		this.selectedRow = selectedRow;
	}

	@Override
	public List<T> getRows() throws SinoPlatformException {
		if (!this.hasLoadFlag || rows == null) {
			this.hasLoadFlag = true;
			this.rows = load();
		}
		return this.rows;
	}

	@Override
	public void setReloadFlag() {
		this.selectedRows = null;
		this.selectedRow = null;
		super.setReloadFlag();
	}

	@Override
	public void onRowSelect(SelectEvent event) {

	}

}
